import $ from "@jquery";

import { onDocumentComplete, watchElement, debounce } from "@utils";

/**
 *对当前document做额外的处理
 * @param {Function} cb
 */
export function handleXiaoniufanyi(cb) {
  onDocumentComplete(document, function () {
    //第一次
    $(document).find("body").autoFixDirection();
    cb(document);

    //body发生改变后的每一次
    watchElement(
      document.body,
      {
        childList: true,
        subtree: true,
      },
      function (mutationsList) {
        for (let mutation of mutationsList) {
          mutation &&
            // $(mutation.target).attr("dir") !== "rtl" &&
            $(mutation.target).autoFixDirection();
        }
      }
    );

    $("#textarea").on(
      "keyup",
      debounce(function () {
        $(this).autoFixDirection();
      }, 500)
    );
  });
}
